package top.sdaily.business.service.engineer.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import top.sdaily.business.dto.DEngineer;
import top.sdaily.business.mapper.engineer.EngineerMapper;
import top.sdaily.business.mapper.engineer.EngineerTagMapper;
import top.sdaily.business.mapper.engineer.TagMapper;
import top.sdaily.business.model.engineer.Engineer;
import top.sdaily.business.model.engineer.EngineerTag;
import top.sdaily.business.model.engineer.Tag;
import top.sdaily.business.service.engineer.EngineerService;
import top.sdaily.business.service.engineer.TagService;
import top.sdaily.core.mybatis.Conditions;
import top.sdaily.core.mybatis.Page;
import top.sdaily.core.service.BaseServiceImpl;
import top.sdaily.core.utils.ArrayUtils;
import top.sdaily.core.utils.IdGeneratorUtils;
import top.sdaily.core.utils.RandomUtils;
import top.sdaily.web.exception.FailedException;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by soya on 2017/2/9.
 */
@Service
@Transactional
public class EngineerServiceImpl extends BaseServiceImpl<Engineer> implements EngineerService {

    @Autowired
    EngineerMapper engineerMapper;
    @Autowired
    TagMapper tagMapper;
    @Autowired
    EngineerTagMapper engineerTagMapper;
    @Autowired
    TagService tagService;

    @Override
    public List<Engineer> getPage(Page page) {
        return engineerMapper.findList(condition().page(page));
    }

    @Override
    public Engineer getById(String id) {
        return engineerMapper.findOne(condition().id(id));
    }

    @Override
    public Engineer getWithTagsById(String id) {
        return engineerMapper.findByIdWithTags(id);
    }

    @Override
    public int create(DEngineer engineer) {
        checkExists(engineer);

        engineer.setPkid(IdGeneratorUtils.fresh());
        engineer.setIsdeleted("0");
        engineer.setSalt(RandomUtils.generateMixString(5));
        engineer.setPassword(DigestUtils.md5DigestAsHex(("000000" + engineer.getSalt()).getBytes()));

        tagService.saveEngineerTag(engineer.getPkid(),engineer.getTagNames());

        Engineer engineeer = new Engineer();
        BeanUtils.copyProperties(engineer,engineeer);
        return engineerMapper.insert(engineeer);
    }

    @Override
    public int modify(DEngineer engineer) {
        tagService.saveEngineerTag(engineer.getPkid(),engineer.getTagNames());

        Engineer engineeer = new Engineer();
        BeanUtils.copyProperties(engineer,engineeer);
        return engineerMapper.updateIgnoreNull(engineeer);
    }

    private void checkExists(DEngineer engineer){
        if(StringUtils.isEmpty(engineer.getPkid())){
            Engineer mobilephone = engineerMapper.findOne(condition().eq("mobilephone", engineer.getMobilePhone()));
            if(mobilephone != null){
                throw new FailedException("手机号已存在");
            }
            Engineer idcard = engineerMapper.findOne(condition().eq("idcard", engineer.getIdCard()));
            if(idcard != null){
                throw new FailedException("身份证号已存在");
            }
        }
    }

}
